﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesBudgetRpt_001 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "DepartmentCode";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                dt = new BudgetBll().Departmentlist(base.CurrentUserID.ToString());
                ddlDepartment.DataSource = dt;
                ddlDepartment.DataTextField = "Departmentname";
                ddlDepartment.DataValueField = "Departmentname";
                ddlDepartment.DataBind();
                ddlDepartment.Items.Add(new ListItem("--不限--", "0"));
                ddlDepartment.SelectedValue = "0";

                dt = new BudgetBll().Saleslist(base.CurrentUserID.ToString());
                ddlSales.DataSource = dt;
                ddlSales.DataTextField = "username";
                ddlSales.DataValueField = "username";
                ddlSales.DataBind();
                ddlSales.Items.Add(new ListItem("--不限--", "0"));
                ddlSales.SelectedValue = "0";

                dt = new BudgetBll().ProductCategorylist();
                ddlProductCategory.DataSource = dt;
                ddlProductCategory.DataTextField = "categoryName";
                ddlProductCategory.DataValueField = "categoryName";
                ddlProductCategory.DataBind();
                ddlProductCategory.Items.Add(new ListItem("--不限--", "0"));
                ddlProductCategory.SelectedValue = "0";

                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = ReportViewer1;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath = strRootPath + "Report\\rdlc\\SalesBudgetRpt_RS_001.rdlc";

            strFilePath = "report/rdlc/SalesBudgetRtp_RS_001.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;

            //get dataset  
            BudgetBll bll = new BudgetBll();
            DataSet ds = bll.GetSalesBudgetRptData_001(sqlWhere);

            ds.DataSetName = "BudgetDataSet_vw_SalesBudget_report";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "BudgetDataSet_vw_SalesBudget_report";
            rds.Value = ds.Tables[0];
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportViewer.LocalReport.Refresh();

        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" and (a.[sales] in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            sb.Append(" or a.[sales] ='").Append(base.CurrentUserEntity.UserName).Append("'");
            sb.Append(" or a.[Producter] ='").Append(base.CurrentUserEntity.UserName).Append("'");
            sb.Append(" or a.[ProductManager] ='").Append(base.CurrentUserEntity.UserName).Append("')");

            if (ddlBudgetYear.SelectedValue != string.Empty)
            {
                sb.Append(" and a.[BudgetYear]  = '").Append(ddlBudgetYear.SelectedValue).Append("'");
            }

            if (ddlCompany.SelectedValue != "0")
            {
                sb.Append(" and a.company='").Append(ddlCompany.SelectedValue.ToString()).Append("'");
            }

            if (this.ddlDepartment.SelectedValue != "0")
            {
                sb.Append(" and a.sales in(select UserName from vw_sysUser where DepartmentName='").Append(ddlDepartment.SelectedValue.ToString()).Append("')");
            }

            if (ddlSales.SelectedValue != "0")
            {
                sb.Append(" and a.sales='").Append(ddlSales.SelectedValue.ToString()).Append("'");
            }

            if (this.ddlProductCategory.SelectedValue != "0")
            {
                sb.Append(" and a.productCategoryCode in(select CategoryCode from Cms_Ctrt_InstrumentCategory where FactoryName='").Append(ddlProductCategory.SelectedValue.ToString()).Append("')");
            }

            if (txtCustomerName.Text.Trim() != "")
            {
                sb.Append(" and a.customerID in(").Append("select customerid from cms_ctrt_customer where customername like '%").Append(txtCustomerName.Text.Trim()).Append("%')");
            }

            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {

            DataTable dt = new BudgetBll().Departmentlist(base.CurrentUserID.ToString(), ddlCompany.SelectedValue.ToString());
            ddlDepartment.DataSource = dt;
            ddlDepartment.DataTextField = "Departmentname";
            ddlDepartment.DataValueField = "Departmentname";
            ddlDepartment.DataBind();
            ddlDepartment.Items.Add(new ListItem("--不限--", "0"));
            ddlDepartment.SelectedValue = "0";
        }

    }
}
